package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.entity.NursingProject;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NursingProjectServiceImpl implements NursingProjectService {

    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    /**
     * 根据条件 分页查询护理项目
     * @param name
     * @param status
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageResponse<NursingProjectVo> findByPage(String name, Integer status, Integer pageNum, Integer pageSize) {
//        使用pageHelper设置
        PageHelper.startPage(pageNum,pageSize);
//        进行查询
        List<NursingProjectVo> list = nursingProjectMapper.selectByCondition(name,status);
//        强转page对象
        Page<NursingProjectVo> page = (Page<NursingProjectVo>) list;
        PageResponse<NursingProjectVo> pageResponse = PageResponse.of(page, NursingProjectVo.class);
//        返回PageResponse对象
        return pageResponse;
    }

    /**
     * 新增护理项目
     * @param nursingProject
     * @return
     */
    @Override
    public void save(NursingProject nursingProject) {
        nursingProjectMapper.insert(nursingProject);
    }

    /**
     * 根据id 查询护理项目
     * @param id
     * @return
     */
    @Override
    public NursingProjectVo findById(Long id) {
        return nursingProjectMapper.selectById(id);
    }

    /**
     * 编辑护理项目
     * @param nursingProject
     * @return
     */
    @Override
    public void updateById(NursingProject nursingProject) {
        nursingProjectMapper.updateById(nursingProject);
    }

    /**
     * 根据id 启用禁用状态
     * @param id
     * @return
     */
    @Override
    public void updateStatus(Long id,Integer status) {
        NursingProject nursingProject = new NursingProject();
        nursingProject.setId(id);
        nursingProject.setStatus(status);
        nursingProjectMapper.updateById(nursingProject);
    }

    /**
     * 根据id 删除护理项目
     * @param id
     * @return
     */
    @Override
    public void deleteById(Long id) {
        nursingProjectMapper.deleteById(id);
    }

    @Override
    public List<NursingProjectVo> findByAll() {
        List<NursingProject> list = nursingProjectMapper.selectAll();
        return BeanUtil.copyToList(list,NursingProjectVo.class);
    }
}
